﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;

namespace Theosoft.Mobile.LibService
{
    public class NetworkDAC
    {
        internal EventHandler onResponseCallback;

        internal NetworkDAC(EventHandler ehResponseCallback)
        {
            this.onResponseCallback = ehResponseCallback;
        }

        internal void http_get(string url)
        {
            HttpWebRequest getRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            getRequest.UserAgent = "Windows Phone 7";
            getRequest.Headers["ClientPlatform"] = "Windows";
            RequestState requestState = new RequestState();
            requestState.request = getRequest;

            IAsyncResult result = (IAsyncResult)getRequest.BeginGetResponse(new AsyncCallback(RespCallback), requestState);
        }

        internal void http_post(string url, string postData)
        {
            HttpWebRequest postRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            postRequest.Method = "POST";
            postRequest.ContentType = "application/x-www-form-urlencoded";
            RequestState requestState = new RequestState();
            requestState.request = postRequest;
            requestState.requestData = new StringBuilder(postData);

            IAsyncResult result = (IAsyncResult)postRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), requestState);
        }

        private void GetRequestStreamCallback(IAsyncResult asyncResult)
        {
            try
            {
                RequestState requestState = (RequestState)asyncResult.AsyncState;
                System.IO.Stream postStream = requestState.request.EndGetRequestStream(asyncResult);
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(requestState.requestData.ToString());
                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Close();
                requestState.request.BeginGetResponse(new AsyncCallback(RespCallback), requestState);
            }
            catch (WebException e)
            {
                // Need to handle the exception
            }
        }

        private void RespCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                RequestState requestState = (RequestState)asynchronousResult.AsyncState;
                HttpWebRequest webRequest = requestState.request;
                requestState.response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

                Stream responseStream = requestState.response.GetResponseStream();
                byte[] responseBody = new byte[responseStream.Length];
                responseStream.Read(responseBody, 0, responseBody.Length);
                responseStream.Close();

                this.onResponseCallback.Invoke(this, new objEventArgs(responseBody));

                requestState.response.Close();
            }
            catch (WebException e)
            {
                // Need to handle the exception
            }
        }

        internal class RequestState
        {
            // This class stores the State of the request.
            public const int BUFFER_SIZE = 1024;
            public StringBuilder requestData;
            public byte[] BufferRead;
            public HttpWebRequest request;
            public HttpWebResponse response;
            public Stream streamResponse;
            public RequestState()
            {
                BufferRead = new byte[BUFFER_SIZE];
                requestData = new StringBuilder("");
                request = null;
                streamResponse = null;
            }
        }
    }

    public class objEventArgs : EventArgs
    {
        public object msg;

        public objEventArgs(object msg)
        {
            this.msg = msg;
        }
    }
}
